let express = require('express');
let path = require("path");
let mysql = require('mysql');
let router = express.Router();

router.get('/index', function(req,res){
    if (req.session.username) {
        res.redirect('/map')
    }
    else {
        res.sendFile(path.join(__dirname, '../public/user/index.html'))
    }

})

router.post('/login/check',function(req, res){
    const connection = req.app.get('connection')
    let username = req.body.username
    let password = req.body.password
    console.log("用户名：" + username)
    console.log("密码：" + password)
    let querySelect = "select * from user where username='"+username+"' and password='"+password+"'";
    connection.query(querySelect, function(err, result){
        if (err) throw err;
        if(result.length != 0) {
            console.log("登录成功");
            req.session.username = username
            res.send({code: 0, info: '登录成功'})
        }
        else {
            console.log("用户名或密码错误");
            res.send({code: 1, info: "用户名或密码错误"});
        }
    })
})

router.post('/register/check',function(req,res){
    const connection = req.app.get('connection')
    let username = req.body.username;
    let password = req.body.password;
    let confirm_password = req.body.confirm_password;
    let agree_volunteer = req.body.agree_volunteer;
    console.log("用户名：" + username);
    console.log("密码：" + password);
    console.log("确认密码：" + confirm_password);
    console.log("成为志愿者：" + agree_volunteer);

    let signupInfo;
    signupInfo = [];

    // 检查密码一致
    if (password != confirm_password) {
        console.log("两次密码输入不一样");
        signupInfo.push("两次密码输入不一样");
    }

    // 检查用户名是否存在
    let querySelectUsername = "select * from user where username='"+username+"'";
    connection.query(querySelectUsername, function(err, result){
        if (err) {
            console.log(err)
            console.log("查询用户名失败");
        }
        else if(result.length != 0) {
            console.log("用户名已被使用");
            signupInfo.push("用户名已被使用");
        }
    });

    // 用户信息存入数据库
    let user = [username, password];
    let queryInsert = 'insert into user(username, password) values(?, ?)';
    connection.query(queryInsert, user, function(err, result) {
        if (err) {
            console.log(err)
            console.log("插入数据库失败");
            signupInfo.push("插入数据库失败");
        }

        // 用户成为志愿者
        if (agree_volunteer == "yes") {
            let volunteer = [username, 'online'];
            let query = 'insert into volunteer(username, status) values(?, ?)';
            connection.query(query, volunteer, function(err, result) {
                if (err) {
                    console.log(err)
                    console.log("成为志愿者失败");
                    signupInfo.push("成为志愿者失败");
                }
            });
        }
    });
    if (signupInfo.length != 0) {
        console.log("注册失败");
        res.send({code: 1, info: signupInfo});
    }
    else {
        console.log("注册成功");
        res.send({code: 0, info: '注册成功'})
    }
})

router.get('/checkLogin', (req, res) => {
    if (req.session.username) {
        res.send({
            isLoggedIn: true,
            username: req.session.username
        })
    }
    else {
        res.send({
            isLoggedIn: false
        })
    }
})

// 用户个人信息界面
router.get('/info', (req, res) => {
    req.session.destroy()
    res.redirect('/user/index')
    //res.sendFile(path.join(__dirname, '../public/user/info.html'))
})

module.exports = router;